var formId = 0;
var currentPosition;
var currentPlaceName;
var currentPlaceDescription;

var mp = new BMap.Map("container");          // 创建地图实例
var point = new BMap.Point(112.917869, 28.234309);  // 创建点坐标
mp.centerAndZoom(point, 16);                 // 初始化地图，设置中心点坐标和地图级别


mp.addControl(new BMap.NavigationControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT, type:BMAP_NAVIGATION_CONTROL_ZOOM}));  //右下角，仅包含缩放按钮
mp.addControl(new BMap.MapTypeControl({anchor:BMAP_ANCHOR_TOP_RIGHT}));    //右上角，默认地图控件
mp.enableScrollWheelZoom();    //启用滚轮放大缩小，默认禁用
mp.enableContinuousZoom();    //启用地图惯性拖拽，默认禁用
var contextMenu = new BMap.ContextMenu();
var txtMenuItem = [
    {
        text:'在此添加标注',
        callback:function (p) {
            if (window.parent.checkLogon())
                openCreateMarker(p);
        }
    }
];


for (var i = 0; i < txtMenuItem.length; i++) {
    contextMenu.addItem(new BMap.MenuItem(txtMenuItem[i].text, txtMenuItem[i].callback, 100));
}
mp.addContextMenu(contextMenu);


function openCurrentInfoBox() {
    if (!$("#placeMarkForm" + formId).valid())  return;
    $.post("plaC.html",$("#placeMarkForm" + formId).serialize() );
    mp.getInfoWindow().close();
    for (var i = 0; i < mp.getOverlays().length; i++) {

        var tmpMark = mp.getOverlays()[i];
        if (tmpMark.getPosition() && tmpMark.getPosition().lng == currentPosition.lng && tmpMark.getPosition().lat == currentPosition.lat) {
            var divObj = $("#showMarkDiv").clone().css('display', 'block')[0];

            $(divObj).find("#markName").html(currentPlaceName);
            $(divObj).find("#markDescprtion").html(currentPlaceDescription);
            var divText = divObj.outerHTML;
            var infoWindow1 = new BMap.InfoWindow(divText);
            tmpMark.addEventListener("click", function () {
                currentPosition = this.getPosition();
                this.openInfoWindow(infoWindow1);
            });
            mp.openInfoWindow(infoWindow1, tmpMark.getPosition());
        }
    }


}
function openCreateMarker(p) {


    var marker1 = new BMap.Marker(p);  // 创建标注
    mp.addOverlay(marker1);              // 将标注添加到地图中

    //创建信息窗口
//            var divText = $("#addMarkDiv").clone().css('display', 'block')[0].outerHTML;
    var divObj = $("#addMarkDiv").clone().css('display', 'block')[0];

    $(divObj).find("#placeMarkForm").attr("id", "placeMarkForm" + (++formId));
    $(divObj).find("#hLng").attr("value", p.lng);
    $(divObj).find("#hLat").attr("value", p.lat);
    currentPosition = p;


    var divText = divObj.outerHTML;
    var infoWindow1 = new BMap.InfoWindow(divText);
    marker1.addEventListener("click", function () {
        currentPosition = this.getPosition();
        this.openInfoWindow(infoWindow1);
    });
    mp.openInfoWindow(infoWindow1, p);
    $("#placeMarkForm" + formId).validate();
}

function addMark(pMark) {
    var p = new BMap.Point(pMark.lng, pMark.lat);
    var marker1 = new BMap.Marker(p);  // 创建标注
    mp.addOverlay(marker1);              // 将标注添加到地图中
    marker1.pMark = pMark;
    var infoWindow1 = createInfoWindow(marker1);
}

function createInfoWindow(marker1) {
    var divObj = $("#showMarkDiv").clone().css('display', 'block')[0];

    $(divObj).find("#markName").html(marker1.pMark.name);
    $(divObj).find("#markDescprtion").html(marker1.pMark.description);
    var divText = divObj.outerHTML;
    var infoWindow1 = new BMap.InfoWindow(divText);
    marker1.addEventListener("click", function () {
        currentPosition = this.getPosition();
        this.openInfoWindow(infoWindow1);
    });
    return infoWindow1;
}
$(document).ready(function () {
    $.get("plaLA.html", function (data) {
        for (var i = 0; i < data.placeMarks.length; i++) {
            var pMark = data.placeMarks[i];
            addMark(pMark);
        }

    });


});




